package com.mixsmart.core.xml.sqlmap;

import java.io.File;
import java.io.InputStream;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class SqlMapValidatorTestMain {

	public static void main(String[] args) throws Exception {
		String xmlFile = "sqlmap.xml";

		// 1. Lookup a factory for the W3C XML Schema language
		SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

		// 2. Compile the schema.
		//File schemaLocation = new File(xsdFile);
		//SqlMapValidatorTestMain.class.getResource("/sqlmap.xsd").toURI();
		File file = new File(SqlMapValidatorTestMain.class.getResource("/sqlmap.xsd").getPath());
		Schema schema = factory.newSchema(file);

		// 3. Get a validator from the schema.
		Validator validator = schema.newValidator();

		// 4. Parse the document you want to check.
		InputStream is = SqlMapValidatorTestMain.class.getResourceAsStream("/"+xmlFile);
		Source source = new StreamSource(is);

		// 5. Check the document
		try {
			validator.validate(source);
			System.out.println(xmlFile + " is valid.");

		} catch (SAXException ex) {
			System.out.println(xmlFile + " is not valid because ");
			System.out.println(ex.getMessage());
		}
	}

}
